//
// MaFenetre.cpp for  in /home/goldbe_y//cpp/plazza-2014-bramar_g/Qt
//
// Made by yoni goldberg
// Login   <goldbe_y@epitech.net>
//
// Started on  Sun Apr 17 16:46:17 2011 yoni goldberg
// Last update Sun Jun 12 01:02:28 2011 leo faucon
//

#include "MaFenetre.hpp"
#include "convert.hpp"

MaFenetre::MaFenetre(QApplication &app)
{

  this->setGeometry(250, 250, 850, 250);
  //setFixedSize( , );
  Consigne = new QLabel("Charger un fichier puis appuyer sur 'Calculer' : ");
  Quit = new QPushButton("Quit");
  calculer = new QPushButton("Calculer");
  charger = new QPushButton("Charger");
  graph = new QPushButton("Graphique");

  moy = new QLabel("Moyenne :");
  prob = new QLabel("Probabilite :");

  table = new QTableWidget(1,11, this);
  table2 = new QTableWidget(1,11, this);

  layout = new QGridLayout;

  if (app.arguments().size() > 1)
    {
      Parseur = new Parse(app.arguments().at(1).toStdString());
      if (Parseur->getcoef().size() != Parseur->getval().size())
	{
	  QMessageBox::critical(this, "Erreur", "Fichier incorrect");
	  Parseur = NULL;
	}
      else
	{
	  QString tmp2("Calculer : ");
	  tmp2 += app.arguments().at(1);
	  calculer->setText(tmp2);
	}
    }
  else
    Parseur = NULL;
  math = NULL;

  create_table();
  Place(app);
}

void		MaFenetre::create_table()
{
  QStringList	header;
  header << "0" <<  "1" <<  "2" <<  "3" <<  "4" <<  "5"
	 <<  "6" <<  "7" <<  "8" <<  "9 et +" <<  "TOTAL";
  table->setHorizontalHeaderLabels(header);
  table->horizontalHeader()->setClickable(false);
  table->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
  table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
  header.clear();
  header << "0x";
  table->setVerticalHeaderLabels(header);
  table->verticalHeader()->setClickable(false);
  table->verticalHeader()->setResizeMode(QHeaderView::Stretch);

  for (int i = 0; i < 11; i++)
    {
      item = new QTableWidgetItem();
      item->setText("");
      item->setFlags(item->flags() & ~Qt::ItemIsEditable);
      table->setItem(0,i,item);
    }

  header.clear();
  header << "0" <<  "1" <<  "2" <<  "3" <<  "4" <<  "5"
  	 <<  "6" <<  "7" <<  "8" <<  "9 et +" <<  "TOTAL";
  table2->setHorizontalHeaderLabels(header);
  table2->horizontalHeader()->setClickable(false);
  table2->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
  table2->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
  header.clear();
  header << "Tx";
  table2->setVerticalHeaderLabels(header);
  table2->verticalHeader()->setClickable(false);
  table2->verticalHeader()->setResizeMode(QHeaderView::Stretch);

  for (int i = 0; i < 11; i++)
    {
      item = new QTableWidgetItem();
      item->setText("");
      item->setFlags(item->flags() & ~Qt::ItemIsEditable);
      table2->setItem(0,i,item);
    }
}

void		MaFenetre::Place(QApplication &app)
{
  layout->addWidget(Consigne, 0, 0);
  layout->addWidget(table, 1, 0, 1, 2);
  layout->addWidget(moy,2,0);
  layout->addWidget(prob,3,0);
  layout->addWidget(table2, 4, 0, 1, 2);
  layout->addWidget(charger, 5, 0);
  layout->addWidget(calculer, 5, 1);
  layout->addWidget(Quit, 6, 0, 1, 2);
  QWidget::connect(Quit, SIGNAL(clicked()), &app, SLOT(quit()));
  QWidget::connect(calculer, SIGNAL(clicked()), this, SLOT(fct_calc()));
  QWidget::connect(charger, SIGNAL(clicked()), this, SLOT(fct_table()));
  setLayout(layout);
}

void		MaFenetre::calc()
{
  if (Parseur != NULL)
    {
      math = new Maths(Parseur->getval(), Parseur->getcoef());

      float	tmp = 0;
      for (int i = 0; i < 11; i++)
	{
	  tmp += math->getTx()[i];
	  table->item(0,i)->setText(QString(var_to_string(math->getOx()[i], 0).c_str()));
	}
      table->item(0,10)->setText(QString(var_to_string(tmp, 0).c_str()));
      tmp = 0;
      for (int i = 0; i < 11; i++)
	{
	  tmp += math->getTx()[i];
	  table2->item(0,i)->setText(QString(var_to_string(math->getTx()[i], 1).c_str()));
	}
      table2->item(0,10)->setText(QString(var_to_string(tmp, 0).c_str()));

      std::string tmp2("Moyenne = ");
      tmp2 += var_to_string(math->getMoy(), 2);
      moy->setText(tmp2.c_str());
      tmp2.clear();
      tmp2 = "Probabilite = ";
      tmp2 += var_to_string(math->getProb(), 4);
      prob->setText(tmp2.c_str());
    }
  else
    QMessageBox::critical(this, "Erreur", "Veuillez charger un fichier");
}

void		MaFenetre::fct_calc()
{
  calc();
}

void		MaFenetre::fct_table()
{
  if (Parseur != NULL)
    delete Parseur;
  if (math != NULL)
    delete math;

  QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Fichier Texte(*.txt *.ol *.fe)");

  if (fichier == NULL)
    return;

  std::string tmp = fichier.toStdString();
  Parseur = new Parse(tmp.c_str());

  if (Parseur->getcoef().size() != Parseur->getval().size())
    QMessageBox::critical(this, "Erreur", "Fichier incorrect");
  else
    {
      QString tmp2("Calculer : ");
      size_t found = tmp.find_last_of("/");
      tmp2 += tmp.substr(found+1).c_str();
      calculer->setText(tmp2);
    }
}
